﻿/// <reference path="jquery-1.7.1-vsdoc.js" />
/// <reference path="MyLibrary.js" />

//一个时候的异常。
$(document).ready(function () {
    $("table").width($("#theinput").width() + 6);
    var top = screen.availHeight / 2 - 200;
    var left = (screen.availWidth - $("#theinput").width()) / 2 - 50;
    $("div").offset({ top: top, left: left });
    $("table").hide();
    $("body").keyup(function (event) {
        var key = event.which;
        if (key == 13) {
            alert($("#theinput").val());
        }
    });
    $("body").click(function () {
        $("table").hide();
        $("table tbody").empty();
    });
    $("#send").click(function (event) {
        alert($("#theinput").val());
    });

    $("#theinput").keyup(function (event) {
        var key = event.which;
        if (key == 38 || key == 40 || key == 13)
            return;
        $("tbody").empty();
        var value = $(this).val();
        $("#theinput").data("value", value);
        value = trimStrSpace(value);
        if (value.length != 0) {
            $.get("Baidu.ashx", { input: value }, function (data, textStatus, jqXHR) {
                var i = 0;
                var result = Convert.jsonStringToObject(data);
                if (result.length != 0) $("table").show();
                for (x in result) {
                    i++;
                    var tr = $("<tr></tr>");
                    var td = $("<td></td>");
                    //var span = $("<span>" + value + "</span>");
                    //var b = $("<b>" + result[x] + "</b>");
                    //td.append(span);
                    //td.append(b);
                    if (i > 10) break;
                    lastResult = result[x].replace(value, "<span>" + value + "</span>");
                    var b = "<b>" + lastResult + "</b>";
                    td.html(b);
                    tr.append(td);
                    $("tbody").append(tr);
                }
            });

        }
        else {
            $("table").hide();
        }
    });

    $("tr").focus(function () {
        $("#theinput").val($(this).text());
    });


    //to-do 到头循环的问题
    $("div").keyup(function (event) {
        var key = event.which;
        if (key == 13)
            return;
        var selectIndex = -1;
        var isInit = false;
        var originalColor = "Green";
        var childLength = $("tbody").children().length;
        $("tbody").children().each(function (index, element) {
            var color = $(element).css("background-color");
            if (color == "Green" || color == "rgb(0, 128, 0)") {
                selectIndex = index;
                isInit = true;
            }
            else {
                originalColor = color;
            }
        });

        //        if (!isInit) {
        //            setBackground("tr:eq(0)");
        //        }
        setBackground("tr:eq(" + selectIndex + ")", originalColor);
        if (key == 38) {
            selectIndex--;
        }
        else if (key == 40) {
            selectIndex++;
        }
        //        else if (key == 13) {
        //            $("#theinput").val($("tr:eq(" + selectIndex + ")").text());
        //        }
        if (childLength != 0) {
            if (selectIndex == childLength || selectIndex == -1) {
                var value = $("#theinput").data("value")
                $("#theinput").val(value);
            } else if (selectIndex > childLength - 1) {
                selectIndex = 0;
            } else if (selectIndex < 0) {
                selectIndex = childLength - 1;
            }
        }


        //一个时候的异常。
        if (childLength == 1) {
            var css = $("tr").css("background-color");
            if (css == "Green" || css == "rgb(0, 128, 0)") {
                $("tr").css("background-color", "transparent");
            }
            else {
                setBackground("tr");
            }

        } else {
            setBackground("tr:eq(" + selectIndex + ")");
        }

        if ($("tr:eq(" + selectIndex + ")").length == 1) {
            $("#theinput").val($("tr:eq(" + selectIndex + ")").text());
        }
    });
});

function setBackground(object, color) {
    if (arguments.length == 2) {
        $(object).css("background-color", color);
    }
    else {
        $(object).css("background-color", "Green");
    }

}
